package com.awesome.common.util.device;

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.provider.Settings;

import com.awesome.common.util.file.EncryptUtil;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

/**
 * Author: JFangZ
 * Email: janezhang1901@163.com
 * Date: 2020/11/17 18:45
 * Description:设备相关工具类
 */
public class DeviceUtil {
    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2020/11/17 18:46
     * Description:获取 Android 系统版本
     */
    public static String getAndroidVersion() {
        return Build.VERSION.RELEASE;
    }

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2020/11/17 18:46
     * Description:获取设备品牌
     */
    public static String getBrand() {
        return Build.BRAND;
    }

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2020/11/17 18:46
     * Description:获取设备参数
     */
    public static String getDevice() {
        return Build.DEVICE;
    }

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2020/11/17 18:46
     * Description:获取设备当前开发代号
     */
    public static String getDisplay() {
        return Build.DISPLAY;
    }

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2020/11/17 18:46
     * Description:获取设备硬件名称
     */
    public static String getHardware() {
        return Build.HARDWARE;
    }

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2020/11/17 18:46
     * Description:
     */
    public static String getHost() {
        return Build.HOST;
    }

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2020/11/17 18:46
     * Description:获取设备 Id
     */
    public static String getId() {
        return Build.ID;
    }

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2020/11/17 18:46
     * Description:获取手机制造商
     */
    public static String getManufacturer() {
        return Build.MANUFACTURER;
    }

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2020/11/17 18:46
     * Description:获取手机型号
     */
    public static String getModel() {
        return Build.MODEL;
    }


    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2020/11/17 18:46
     * Description:获取设备的名称
     */
    public static String getProduct() {
        return Build.PRODUCT;
    }

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2020/11/17 18:46
     * Description:获取 SDK 版本
     */
    public static int getSDKVersion() {
        return Build.VERSION.SDK_INT;
    }

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2020/11/17 18:46
     * Description:获取设备描述 Build 的标签
     */
    public static String getTags() {
        return Build.TAGS;
    }

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2020/11/17 18:46
     * Description:获取设备描述 Build 的类型
     */
    public static String getType() {
        return Build.TYPE;
    }

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2020/11/17 18:46
     * Description:获取设备描述 Build 的 USER
     */
    public static String getUser() {
        return Build.USER;
    }


    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2020/11/17 18:46
     * Description:获取设备网卡唯一标识
     */
    public static String getMac(Context context) {
        String mac = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            // Android 7.0 以上
            try {
                Enumeration<NetworkInterface> networkInterfaceEnumeration = NetworkInterface.getNetworkInterfaces();
                while (networkInterfaceEnumeration.hasMoreElements()) {
                    NetworkInterface networkInterface = networkInterfaceEnumeration.nextElement();
                    if (!networkInterface.getName().equalsIgnoreCase("wlan0")) {
                        continue;
                    }
                    byte[] bytes = networkInterface.getHardwareAddress();
                    if (bytes != null) {
                        StringBuilder stringBuilder = new StringBuilder();
                        for (byte b : bytes) {
                            stringBuilder.append(String.format("%02X:", b));
                        }
                        if (stringBuilder.length() > 0) {
                            stringBuilder.deleteCharAt(stringBuilder.length() - 1);
                        }
                        mac = stringBuilder.toString();
                    }
                }
            } catch (SocketException e) {
                e.printStackTrace();
            }
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            // Android 6.0 以上
            String str = "";
            try {
                Process process = Runtime.getRuntime().exec("cat/sys/class/net/wlan0/address");
                InputStreamReader inputStreamReader = new InputStreamReader(process.getInputStream());
                LineNumberReader lineNumberReader = new LineNumberReader(inputStreamReader);
                while (str != null) {
                    str = lineNumberReader.readLine();
                    if (str != null) {
                        mac = str.trim();//去空格
                        break;
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
            if (wifiManager != null) {
                WifiInfo wifiInfo = wifiManager.getConnectionInfo();
                if (wifiInfo != null) {
                    mac = wifiInfo.getMacAddress();
                }
            }
        }
        return mac;
    }

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2020/11/17 18:46
     * Description:获取 AndroidId
     */
    public static String getAndroidId(Context context) {
        return Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
    }

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2020/12/16 11:24
     * Description:获取设备唯一标识，通过硬件信息拼的，可能会变化
     */
    public static String getDeviceId(Context context) {
        String deviceInfo = getBrand() + "_"
                + getDevice() + "_"
                + getDisplay() + "_"
                + getHardware() + "_"
                + getHost() + "_"
                + getId() + "_"
                + getManufacturer() + "_"
                + getModel() + "_"
                + getProduct() + "_"
                + getTags() + "_"
                + getType() + "_"
                + getUser() + "_"
                + getMac(context);
        return EncryptUtil.sha1Encrypt(deviceInfo.getBytes());
    }
}
